//
// Created by china on 25-1-10.
//

#include "GroupModel.h"

bool GroupModel::createGroup(MySQLTable::AllGroup &group) {
    LOG_INFO<<"GroupModel::createGroup";

    if(group.name.empty()){
        LOG_ERROR<<"不是有效信息";
        return false;
    }

    char sql[512] = {0};
    sprintf(sql,"INSERT INTO allgroup(groupname,groupdesc) VALUES('%s','%s')",\
            group.name.c_str(),group.desc.c_str());

    auto pool = Pool::getInstance();
    auto conn = pool->getFreeConn();
    if(conn->update(sql)){
        group.id = mysql_insert_id(conn->getConn());
        return true;
    }
    return false;
}

bool GroupModel::addGroup(const MySQLTable::GroupUser &groupUser) {
    LOG_INFO<<"GroupModel::addGroup";

    if(groupUser.groupid == -1 || groupUser.user.id == -1){
        LOG_ERROR<<"不是有效信息";
        return false;
    }

    char sql[512] = {0};
    sprintf(sql,"INSERT INTO groupuser VALUES(%d,%d,'%s')",\
            groupUser.groupid,groupUser.user.id,groupUser.role.c_str());

    auto pool = Pool::getInstance();
    auto conn = pool->getFreeConn();
    if(conn->update(sql)){
        return true;
    }

    return false;
}

std::vector<struct MySQLTable::AllGroup> GroupModel::queryGroups(const MySQLTable::User &user) {
    LOG_INFO<<"GroupModel::queryGroups";

    std::vector<struct MySQLTable::AllGroup> groupVec;
    if(user.id == -1){
        LOG_ERROR<<"不是有效信息";
        return groupVec;
    }

    char sql[1024] = {0};
    sprintf(sql,"SELECT a.id,a.groupname,a.groupdesc FROM allgroup a INNER JOIN groupuser b ON a.id = b.groupid WHERE b.userid = %d",\
            user.id);

    auto pool = Pool::getInstance();
    auto conn = pool->getFreeConn();
    MYSQL_RES* res = conn->query(sql);
    if(res != nullptr){
        MYSQL_ROW row;
        while((row = mysql_fetch_row(res)) != nullptr){
            struct MySQLTable::AllGroup group;
            group.id = atoi(row[0]);
            group.name = row[1];
            group.desc = row[2];
            groupVec.push_back(group);
        }
    }
    //释放资源
    mysql_free_result(res);

    //查询群里面的用户信息
    for(struct MySQLTable::AllGroup &group : groupVec){
        sprintf(sql,"SELECT a.id,a.name,a.state,b.grouprole FROM user a INNER JOIN groupuser b ON b.userid = a.id \ "
                    "WHERE b.groupid=%d",group.id);
        MYSQL_RES *res2 = conn->query(sql);
        if(res2 != nullptr){
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res2)) != nullptr){
                struct MySQLTable::GroupUser groupUser;
                groupUser.user.id = atoi(row[0]);
                groupUser.user.name = row[1];
                groupUser.user.state = row[3];
                groupUser.role = row[4];
                group.users.push_back(groupUser);
            }
        }
        //释放资源
        mysql_free_result(res2);
    }
    return groupVec;
}


std::vector<int> GroupModel::queryGroupUsers(int userid, int groupid) {
    LOG_INFO<<"GroupModel::queryGroupUsers";

    char sql[512] = {0};
    sprintf(sql,"SELECT userid FROM groupuser WHERE groupid=%d AND userid!=%d",groupid,userid);
    vector<int> idVec;

    auto pool = Pool::getInstance();
    auto conn = pool->getFreeConn();
    MYSQL_RES* res = conn->query(sql);
    if(res != nullptr){
        MYSQL_ROW row;
        while((row = mysql_fetch_row(res)) != nullptr){
            idVec.push_back(atoi(row[0]));
        }
    }
    //释放资源
    mysql_free_result(res);
    return idVec;
}
